package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ROBBED_NOTIFY_FROM_CROSS, desc = "被劫(镖)通知")
public class BeautyEscortRobbedNotifyCmdFromCross implements Command {

    List<Long> readUserId = new ArrayList<>();

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortRobbedNotifyMsg notifyMsg = BeautyEscortProto.BeautyEscortRobbedNotifyMsg.parseFrom(packet.getBytes());
        UserInfo userInfo = UserMgr.getUserInfo(notifyMsg.getRobbedUserId());
        if(userInfo != null){
            if(!StringUtils.isNullOrEmpty(userInfo.getUnionUid())){
                Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(userInfo.getUnionUid());
                if(unionMemberMap != null){
                    for (Map.Entry<Long, CrossUnionMember> memberEntry : unionMemberMap.entrySet()) {
                        sendNotifyMsg(notifyMsg, memberEntry.getKey());
                    }
                }else {
                    sendNotifyMsg(notifyMsg, userInfo.getUserId());
                }
            }else {
                sendNotifyMsg(notifyMsg, userInfo.getUserId());
            }

            if(readUserId.size() > 0) {
                //返回跨服
                BeautyEscortProto.CrossBeautyEscortRobbedNotifyBackMsg.Builder backMsg = BeautyEscortProto.CrossBeautyEscortRobbedNotifyBackMsg.newBuilder();
                backMsg.addAllReadUser(readUserId);
                backMsg.setTime(notifyMsg.getRobbedTime());
                backMsg.setRobbedUserId(notifyMsg.getRobbedUserId());
                GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_BEAUTY_ESCORT_ROBBED_NOTIFY_BACK, backMsg));
            }
        }
    }

    private void sendNotifyMsg(BeautyEscortProto.BeautyEscortRobbedNotifyMsg notifyMsg, long userId) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {
            readUserId.add(player.getUserId());

            BeautyEscortProto.BeautyEscortRobbedNotifyMsg.Builder notifyMsg1 = BeautyEscortProto.BeautyEscortRobbedNotifyMsg.newBuilder();
            notifyMsg1.setUserBaseInfo(notifyMsg.getUserBaseInfo());
            notifyMsg1.setServerId(notifyMsg.getServerId());
            notifyMsg1.setRobUserId(notifyMsg.getRobUserId());
            notifyMsg1.setRobbedUserId(notifyMsg.getRobbedUserId());
            notifyMsg1.setRobbedUserName(notifyMsg.getRobbedUserName());
            notifyMsg1.setRobbedLoss(notifyMsg.getRobbedLoss());
            notifyMsg1.setRobbedTime(notifyMsg.getRobbedTime());
            notifyMsg1.setDartId(notifyMsg.getDartId());
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROBBED_NOTIFY, notifyMsg1);
            //BeautyEscortMgr.getLogger().info("被劫(镖)通知: userId:{},unionUid:{},loss:{}", player.getUserId(), player.getUserInfo().getUnionUid(), notifyMsg.getRobbedLoss());

            //红点
            BeautyEscortProto.BeautyEscortRedDotSyncMsg.Builder redDotMsg = BeautyEscortProto.BeautyEscortRedDotSyncMsg.newBuilder();
            if(player.getUserId() == userId){
                redDotMsg.setReport(1);
            }else {
                redDotMsg.setUnionReport(1);
            }
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_RED_DOT_SYNC, redDotMsg);
        }
    }

}
